package com.csist.a15classone.cistamis;

import android.content.Context;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.csist.a15classone.cistamis.utils.PreUtils;

import java.util.ArrayList;

public class GuildActivity extends AppCompatActivity {
    private Context context = GuildActivity.this;
    private ViewPager viewPager;
    private LinearLayout linearLayout;
    private int[] imageResIDs;
    private ArrayList<ImageView> imageViews;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_guild);
        //更行sp中的is_first_enter,设置为false，确保下次直接进入登录界面
        PreUtils.setBoolean(context,"is_first_enter",false);

        //初始化界面 v：view
        initView();
        //数据 model
        initData();
        //控制器 controller
        initAdapter();

    }

    private void initView() {
        viewPager = (ViewPager) findViewById(R.id.viewPager);
        linearLayout = (LinearLayout) findViewById(R.id.ll_dots);
    }

    private void initData() {
        //图片资源郑和  创建图片ID的数组
        imageResIDs = new int [] {R.drawable.a,R.drawable.b,R.drawable.c,R.drawable.b};
        //将图片转换为imageview对象
        ImageView imageView ;
        imageViews = new ArrayList<>();
        for (int i = 0; i < imageResIDs.length; i++) {
            imageView = new ImageView(GuildActivity.this);
            imageView.setBackgroundResource(imageResIDs[i]);
            imageViews.add(imageView);
        }

    }


    private void initAdapter() {
        viewPager.setAdapter(new MyAdapter());
    }

    private class MyAdapter extends PagerAdapter {

        @Override
        public int getCount() {
            return imageViews.size();
        }

        @Override
        public boolean isViewFromObject(View view, Object object) {
            return view == object;
        }

        @Override
        public Object instantiateItem(ViewGroup container, int position) {
            container.addView(imageViews.get(position));
            return imageViews.get(position);
        }

        @Override
        public void destroyItem(ViewGroup container, int position, Object object) {
            container.removeView((View) object  );
        }
    }
}
